package com.example.carmessage2;

import android.app.ActivityOptions;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.hardware.display.DisplayManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class CarMessageFu extends AppCompatActivity {

   Button baoyangjianyi,tanyajiance,yesbaocun;
   ImageView loding,lishijilu,imgcar,fuback;
   TextView textloding,leftqian ,lefthou ,rightqian, righthou;

   int save;
   int shutdow;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_message_fu);
        baoyangjianyi=findViewById(R.id.baoyangjianyi);
        lishijilu=findViewById(R.id.lishijilu);
        tanyajiance=findViewById(R.id.tanyajiance);
        loding=findViewById(R.id.loding);
        textloding=findViewById(R.id.textloding);
        imgcar=findViewById(R.id.imgcar);
        yesbaocun=findViewById(R.id.yesbaocun);
        fuback=findViewById(R.id.fuback);


        leftqian=findViewById(R.id.leftqian);
        lefthou=findViewById(R.id.lefthou);

        rightqian=findViewById(R.id.rightqian);
        righthou=findViewById(R.id.righthou);



        fuback.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
            @Override
            public void onClick(View view) {
                  System.exit(0);
                  shutdow=1;
                getZhuPing();
            }
        });

        lishijilu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (save==1 ){

                    Intent intent=new Intent(CarMessageFu.this,Histroycord.class);
                    startActivity(intent);
                }else {
                    Toast.makeText(CarMessageFu.this, "暂无数据", Toast.LENGTH_SHORT).show();
                }

            }
        });
            tanyajiance.setOnClickListener(new View.OnClickListener() {
                @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
                @Override
                public void onClick(View view) {

                    loding.setVisibility(View.VISIBLE);

             AnimationDrawable xx= (AnimationDrawable) loding.getBackground();
             xx.start();

                    textloding.setVisibility(View.VISIBLE);
                    int left=5;


                    Handler handler=new Handler();
                    Runnable runnable=new Runnable() {
                        @Override
                        public void run() {
                            loding.setVisibility(View.GONE);
                            textloding.setVisibility(View.GONE);

                            yesbaocun.setVisibility(View.VISIBLE);
                            baoyangjianyi.setVisibility(View.VISIBLE);
                            imgcar.setBackgroundResource(R.drawable.cartaiya);


                            leftqian.setText("2.0"+"\n"+"\n"+55.0+"℃");
                            lefthou.setText("3.0"+"\n"+"\n"+60.0+"℃");
                            rightqian.setText("5.0"+"\n"+"\n"+58.0+"℃");
                            righthou.setText("4.0"+"\n"+"\n"+59.0+"℃");
                            righthou.setTextColor(Color.RED);

                        }
                    }; handler.postDelayed(runnable,2000);


                    if (left==5){


                        Handler handler2=new Handler();
                        Runnable runnable1=new Runnable() {
                            @Override
                            public void run() {


                                AlertDialog.Builder builder=new AlertDialog.Builder(CarMessageFu.this);
                                   builder.setTitle("提示：").setMessage("右后轮胎胎压过高，请联系维保人员")
                                              .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                                  @Override
                                                  public void onClick(DialogInterface dialogInterface, int i) {
                                                      Toast.makeText(CarMessageFu.this,"已取消",Toast.LENGTH_SHORT).show();
                                                  }
                                              })
                                              .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                          @Override
                                          public void onClick(DialogInterface dialogInterface, int i) {
                                                      Toast.makeText(CarMessageFu.this,"已确定",Toast.LENGTH_SHORT).show();
                                          }
                                      });
                                      builder.show();
                                getYiBiao();
                            }
                        };handler2.postDelayed(runnable1,3000);

                    }else {


                    }

                }
            });
            baoyangjianyi.setOnClickListener(new View.OnClickListener() {
                @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
                @Override
                public void onClick(View view) {
                  
                    BaoyanAdviceFuPing("保养建议1");
                }
            });
        yesbaocun.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
            @Override
            public void onClick(View view) {

                Toast.makeText(CarMessageFu.this,"已保存监测数据到历史记录",Toast.LENGTH_SHORT).show();
                 save=1;
                HisPuping();
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void getZhuPing() {

        DisplayManager displayManager = (DisplayManager) CarMessageFu.this.getSystemService(Context.DISPLAY_SERVICE);
        Display[] displays = displayManager.getDisplays();

        if (displays.length > 1) {
            Intent intent = new Intent(CarMessageFu.this, Carmessage.class);

            intent.putExtra("shutdown",shutdow);
            intent.setClassName(getPackageName(), getPackageName() + ".Carmessage");

            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

            ActivityOptions options = null;

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                options = ActivityOptions.makeBasic();
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                options.setLaunchDisplayId(displays[0].getDisplayId());
            }
            startActivity(intent, options.toBundle());
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void getYiBiao() {

        DisplayManager displayManager = (DisplayManager) CarMessageFu.this.getSystemService(Context.DISPLAY_SERVICE);
        Display[] displays = displayManager.getDisplays();

        if (displays.length > 1) {
            Intent intent = new Intent(CarMessageFu.this, YiBiaoPan.class);
            intent.putExtra("baojing",true );
            intent.putExtra("shutdown",shutdow);
            intent.setClassName(getPackageName(), getPackageName() + ".YiBiaoPan");

            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

            ActivityOptions options = null;

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                options = ActivityOptions.makeBasic();
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                options.setLaunchDisplayId(displays[1].getDisplayId());
            }
            startActivity(intent, options.toBundle());
        }
    }


    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void BaoyanAdviceFuPing(String s) {
        DisplayManager displayManager= (DisplayManager) CarMessageFu.this.getSystemService(Context.DISPLAY_SERVICE);
        Display[]displays=displayManager.getDisplays();

        if (displays.length>1){
            Intent intent=new Intent(CarMessageFu.this, ProtetdAdvice.class);
             intent.putExtra("advice1",s);

            intent.setClassName(getPackageName(),getPackageName()+".ProtetdAdvice");

            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

            ActivityOptions options=null;

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                options=ActivityOptions.makeBasic();
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                options.setLaunchDisplayId(displays[1].getDisplayId());
            }
            startActivity(intent,options.toBundle());
        }

    }


    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void HisPuping() {
        DisplayManager displayManager= (DisplayManager) CarMessageFu.this.getSystemService(Context.DISPLAY_SERVICE);
        Display[]displays=displayManager.getDisplays();

        if (displays.length>1){
            Intent intent=new Intent(CarMessageFu.this, Histroycord.class);


            intent.setClassName(getPackageName(),getPackageName()+".Histroycord");

            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

            ActivityOptions options=null;

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                options=ActivityOptions.makeBasic();
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                options.setLaunchDisplayId(displays[1].getDisplayId());
            }
            startActivity(intent,options.toBundle());
        }

    }
}